package com.assignwork.service.impl;

import com.assignwork.dto.CorpCargoSearchDTO;
import com.assignwork.dto.CorpCargoSubmitDTO;
import com.assignwork.entity.CorpCargo;
import com.assignwork.entity.ShipAgent;
import com.assignwork.mapper.CorpCargoMapper;
import com.assignwork.service.ICorpCargoService;
import com.assignwork.util.PinyinUtil;
import com.assignwork.vo.CorpCargoListVO;
import com.assignwork.vo.CorpCargoPageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName CorpCargoServiceImpl
 * @Description
 * @Author dujianyu
 * @Date 2023/8/22 15:27
 */
@Service
public class CorpCargoServiceImpl extends BaseServiceImpl<CorpCargoMapper, CorpCargo> implements ICorpCargoService {
    @Override
    public IPage<CorpCargoPageVO> selectCorpCargoPage(CorpCargoSearchDTO dto, IPage<CorpCargoPageVO> page) {
        List<CorpCargoPageVO> data = baseMapper.selectCorpCargoPage(dto, page);
        return page.setRecords(data);
    }

    @Override
    public List<CorpCargoListVO> selectCorpCargoList(CorpCargoSearchDTO dto) {
        List<CorpCargoListVO> data = baseMapper.selectCorpCargoList(dto);
        for (CorpCargoListVO vo : data) {
            vo.setCargoSearchName(vo.getCargoName() + "(" + vo.getPinyinHead() + ")");
        }
        return data;
    }

    @Override
    public Boolean submitCorpCargo(CorpCargoSubmitDTO dto) {
        if (Func.isNull(dto.getId())) {
            QueryWrapper<CorpCargo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CorpCargo::getIsDeleted, 0).eq(CorpCargo::getCargoCode, dto.getCargoCode());
            if (baseMapper.selectCount(queryWrapper) > 0) {
                throw new RuntimeException("编码重复");
            }
        }
        dto.setPinyinHead(PinyinUtil.getPinYinHeaderChar(dto.getCargoName()));
        CorpCargo entity = BeanUtil.copy(dto, CorpCargo.class);
        return super.saveOrUpdate(entity);
    }

    @Override
    public Map<String, String> corpCargoMap() {
        CorpCargoSearchDTO dto = new CorpCargoSearchDTO();
        List<CorpCargoListVO> data = selectCorpCargoList(dto);
        return data.stream().collect(Collectors.toMap(CorpCargoListVO::getCargoCode, CorpCargoListVO::getCargoName, (key2, key1) -> key1));
    }

    @Override
    public boolean pinyinSet() {
        QueryWrapper<CorpCargo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CorpCargo::getIsDeleted, 0);
        List<CorpCargo> corpCargos = baseMapper.selectList(queryWrapper);
        for (CorpCargo corpCargo : corpCargos) {
            corpCargo.setPinyinHead(PinyinUtil.getPinYinHeaderChar(corpCargo.getCargoName()));
        }
        return super.saveOrUpdateBatch(corpCargos);
    }
}
